package com.elitel.frame.main.entity.vo;

import java.io.Serializable;

import com.elitel.frame.main.entity.enums.ResponseStatusCodeEnum;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
 * 新数据接口返回包装类
 * @author dingdaifu
 *
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class JsonDataResult implements Serializable {

	private static final long serialVersionUID = -7977264132527482387L;

	private Integer result;

	private String message;

	private String responseMode;

	private Object data;


	public static JsonDataResult responseSuccess(Object data){
		return responseSuccess(data, null);
	}

	public static JsonDataResult responseSuccess(Object data, Integer responseMode){
		return builder()
			.result(ResponseStatusCodeEnum.SUCCESS.statusCode())
			.message("操作成功")
			.responseMode(responseMode)
			.data(data).build();
	}

	public static JsonDataResult responseError(String content){
		return responseError(content, null);
	}

	public static JsonDataResult responseError(String content, Integer responseMode){
		return responseError(ResponseStatusCodeEnum.ERROR, content, responseMode);
	}
	public static JsonDataResult responseError(ResponseStatusCodeEnum responseStatus,  String content, Integer responseMode){
		return builder()
			.result(responseStatus.statusCode())
			.responseMode(responseMode)
			.message(content).build();
	}

	public static JsonDataResult responseParameterError(String content){
		return builder()
			.result(ResponseStatusCodeEnum.PARAMETER_ERROR.statusCode())
			.message(isNotEmpty(content) ? content : "参数错误").build();
	}

	public static boolean isNotEmpty(String content){
		return !isEmpty(content);

	}

	public static boolean isEmpty(String content){
		return content == null || content.isEmpty();
	}

	public static final JsonDataResultBuilder builder(){
		return new JsonDataResultBuilder();
	}

	// Here is builder to build JsonDataResult
	public static final class JsonDataResultBuilder{

		private static final String ResponseMode_SingleRow = "SingleMode";
		private static final String ResponseMode_MultipleRow = "MultipleMode";
		private static final String ResponseMode_UnKnownResponse = "UnKnown";


		public static String convertResponseMode(Integer responseMode){
			if(responseMode == null)
				return ResponseMode_UnKnownResponse;

			return responseMode == 1 ? ResponseMode_SingleRow : ResponseMode_MultipleRow;
		}

		private JsonDataResultBuilder jsonDataResultBuilder;

		private Integer result;

		private String message;

		private Integer responseMode;

		private Object data;

		public JsonDataResultBuilder(){
			jsonDataResultBuilder = this;
		}

		public JsonDataResultBuilder result(Integer result){
			this.result = result;
			return jsonDataResultBuilder;
		}

		public JsonDataResultBuilder message(String message){
			this.message = message;
			return jsonDataResultBuilder;
		}

		public JsonDataResultBuilder responseMode(Integer responseMode){
			this.responseMode = responseMode;
			return jsonDataResultBuilder;

		}

		public JsonDataResultBuilder data(Object data){
			this.data = data;
			return jsonDataResultBuilder;
		}

		public JsonDataResult build(){
			return new JsonDataResult(this.result, this.message, convertResponseMode(this.responseMode), this.data);
		}
	}

}
